package com.letcoding.spiderAutoHome.config;

import com.letcoding.spiderAutoHome.job.CloseHttpConnectionJob;
import com.letcoding.spiderAutoHome.job.CrawlerAutoHomeJob;
import org.quartz.CronTrigger;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

/**
 * @ClassName QuartzConfig
 * @Description 定时任务配置
 * @Author will
 * @Date 2021/5/6 17:01
 * @Company 北京华焱坤泰科技有限公司
 */
@Configuration
public class QuartzConfig {
    /**
     * 定义关闭无效连接任务
     */
    @Bean("closeHttpConnectionJob")
    public JobDetailFactoryBean closeHttpConnectionJob() {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setApplicationContextJobDataKey("context");
        jobDetailFactoryBean.setJobClass(CloseHttpConnectionJob.class);
        jobDetailFactoryBean.setDurability(true);

        return jobDetailFactoryBean;
    }

    /**
     * 定义关闭无效连接触发器
     */
    @Bean("closeHttpConnectionJobTrigger")
    public CronTriggerFactoryBean closeHttpConnectionJobTrigger(
            @Qualifier(value = "closeHttpConnectionJob") JobDetailFactoryBean itemJobBean) {
        CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
        tigger.setJobDetail(itemJobBean.getObject());
        tigger.setCronExpression("0/5 * * * * ? ");
        return tigger;
    }


    /**
     * 定义定时爬取评测任务任务
     */
    @Bean("crawlerAutoHomeJob")
    public JobDetailFactoryBean crawlerAutoHomeJob() {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setApplicationContextJobDataKey("context");
        jobDetailFactoryBean.setJobClass(CrawlerAutoHomeJob.class);
        jobDetailFactoryBean.setDurability(true);

        return jobDetailFactoryBean;
    }

    /**
     * 定义关闭无效连接触发器
     */
    @Bean("crawlerAutoHomeJobTrigger")
    public CronTriggerFactoryBean crawlerAutoHomeJobTrigger(
            @Qualifier(value = "crawlerAutoHomeJob") JobDetailFactoryBean itemJobBean) {
        CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
        tigger.setJobDetail(itemJobBean.getObject());
        tigger.setCronExpression("0/5 * * * * ? ");
        return tigger;
    }

    /**
     * 定义调度器
     */
    @Bean
    public SchedulerFactoryBean schedulerFactory(CronTrigger[] cronTriggerImpl) {
        SchedulerFactoryBean bean = new SchedulerFactoryBean();
        bean.setTriggers(cronTriggerImpl);
        return bean;
    }


}
